package com.tcj.blog.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tcj.blog.controller.ControllerUtil;
import com.tcj.blog.domain.MessageBoard;
import com.tcj.blog.mapper.MessageBoardMapper;
import com.tcj.blog.service.IMessageBoardService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tcj.blog.vo.MessageBoardVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Tcj
 * @since 2020-12-02
 */
@Service
public class MessageBoardServiceImpl extends ServiceImpl<MessageBoardMapper, MessageBoard> implements IMessageBoardService {
    @Autowired
    MessageBoardMapper messageBoardMapper;
    @Override
    public List<String> getRole(int id) {
        return messageBoardMapper.getRole(id);
    }

    @Override
    public IPage<MessageBoardVO> listMessageBordDataByPage(int pageIndex, int pageSize,int blogId) {
        IPage<MessageBoardVO> iPage = new Page<>();
        IPage<MessageBoard> page = new Page<>(pageIndex,pageSize);
        QueryWrapper<MessageBoard> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("blog_id",blogId);
        queryWrapper.eq("parent_id",0);
        queryWrapper.orderByDesc("create_time");
        IPage<MessageBoard> messageBoardIPage = messageBoardMapper.selectPage(page, queryWrapper);
        List<MessageBoardVO> messageBoardVOS = new ArrayList<>();
        for (MessageBoard record : messageBoardIPage.getRecords()) {
            MessageBoardVO messageBoardVO = (MessageBoardVO) ControllerUtil.copyData(record, new MessageBoardVO());
            messageBoardVO.setMessageRole(messageBoardMapper.getRole(messageBoardVO.getUserId()));
            messageBoardVO.setSubMessageBoard(getSubMessage(messageBoardVO));
            messageBoardVOS.add(messageBoardVO);
        }
        iPage = (IPage<MessageBoardVO>) ControllerUtil.copyData(page,iPage);
        iPage.setRecords(messageBoardVOS);
        return iPage;
    }
    private List<MessageBoardVO> getSubMessage(MessageBoardVO messageBoardVO){
        List<MessageBoardVO> messageBoardVOS = new ArrayList<>();
        QueryWrapper<MessageBoard> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("parent_id",messageBoardVO.getId());
        List<MessageBoard> messageBoards = messageBoardMapper.selectList(queryWrapper);
        for (MessageBoard messageBoard : messageBoards) {
            MessageBoardVO messageBoardvo = (MessageBoardVO) ControllerUtil.copyData(messageBoard, new MessageBoardVO());
            messageBoardvo.setMessageRole(messageBoardMapper.getRole(messageBoard.getId()));
            messageBoardvo.setSubMessageBoard(getSubMessage(messageBoardvo));
            messageBoardVOS.add(messageBoardvo);
        }
        return messageBoardVOS;
    }
}
